package com.ruoyi.premises.controller;

import com.bw.springboot.starter.upload.model.Chunk;
import com.bw.springboot.starter.upload.service.FileService;
import com.bw.springboot.starter.upload.util.FileResult;
import com.ruoyi.premises.domain.Result;
import com.ruoyi.premises.domain.SysOss;
import com.ruoyi.premises.service.ISysOssService;
import com.ruoyi.premises.utils.QiniuKodoFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("qiniukodo")
@Slf4j
@CrossOrigin
public class QiniuKodoController {
    @Resource
    QiniuKodoFile qiniuKodoUtil;
    //多个图片上传
    @RequestMapping("uploads")
    public Map<String, List<String>> uploads(MultipartFile[] localFilePath) throws Exception {
        Map<String, List<String>> upload = qiniuKodoUtil.uploadImages(localFilePath);
        return upload;
    }
    @Resource
    ISysOssService service;
    //单个图片上传
    @RequestMapping("upload")
    public Map upload(MultipartFile file) throws Exception {
        String upload = qiniuKodoUtil.uploadFile(file);
        System.out.println(upload);
        Map map = new HashMap();
        map.put("code",200);
        map.put("url",upload);
        return map;
    }

    @Autowired
    FileService fileService;
    /**
     * 多文件上传
     * @param chunk
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/fileUpload")
    public Result postFileUpload(@ModelAttribute Chunk chunk)  {
        fileService.postFileUpload(chunk);
        if (chunk.getTotalChunks().equals(chunk.getChunkNumber())) {
            FileResult fileResult = fileService.mergeFile(chunk);
            System.err.println("=============>文件上传成功");
            return Result.success(fileResult);
        } else {
            return Result.fail();
        }
    }
    //获取图片
    @RequestMapping("getFileUrl")
    public void getFileUrl(String fileName) {
        try {
            qiniuKodoUtil.getFileUrl(fileName);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    //删除图片
    @RequestMapping("deleteFile")
    public void deleteFile(String[] fileList) {
        qiniuKodoUtil.deleteFile(fileList);
    }

    @RequestMapping("listSpaceFiles")
    public void listSpaceFiles() {
        qiniuKodoUtil.listSpaceFiles();
    }

}
